﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChanExt;
using C.Entities;

namespace C.Unite
{
    /// <summary>
    /// 全局继承页
    /// </summary>
    public class CommonPageBase : System.Web.UI.Page
    {
        #region 初始化
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            #region 验证登录状态
            string cookname = ConfigHelper.GetWebConfigPath("LoginCookGroup");
            bool yz = true;
            // PageContext.RegisterStartupScript(Alert.GetShowInTopReference("超时登录", "提示", "top.location.href ='/tbs_Login.aspx'"));
            if (System.Web.HttpContext.Current.Request.Cookies[cookname] != null && System.Web.HttpContext.Current.Request.Cookies[cookname].Value != null && System.Web.HttpContext.Current.Request.Cookies[cookname].Value != "")
            {
                if (Request.Cookies[cookname]["Uid"] == null || Request.Cookies[cookname]["NewGroupID"] == null || Request.Cookies[cookname]["GroupName"] == null || Request.Cookies[cookname]["TrueName"] == null)
                {
                    yz = false;
                }
            }
            else
            {
                yz = false;
            }

            if (yz == false)
            {
                Response.Write("<script language='javascript'>alert('超时登录！');top.location.href ='/Login.aspx';</script>");
                Response.End();
            }
            #endregion
            //this.Error += new System.EventHandler(PageBase_Error);
            if (!IsPostBack)
            {
                #region 更换页面皮肤
                if (PageManager.Instance != null)
                {
                    HttpCookie themeCookie = Request.Cookies["Theme"];
                    if (themeCookie != null)
                    {
                        string themeValue = themeCookie.Value;

                        if (IsSystemTheme(themeValue))
                        {
                            PageManager.Instance.Theme = (Theme)Enum.Parse(typeof(Theme), themeValue, true);
                        }
                        else
                        {
                            PageManager.Instance.CustomTheme = themeValue;
                        }
                    }
                }
                #endregion
            }
        }
        #endregion

        #region 设定空值
        protected void ShowNoData(Grid gd)
        {
            gd.EmptyText = String.Format("<div style=\"background: url({0}) center no-repeat;height: 50px;\"></div>", ResolveUrl("~/images/datanone.png"));
        } 
        #endregion

        #region 是否有系统默认皮肤
        /// <summary>
        /// 是否有系统默认皮肤
        /// </summary>
        /// <param name="themeName">皮肤名</param>
        /// <returns></returns>
        private bool IsSystemTheme(string themeName)
        {
            string[] themes = Enum.GetNames(typeof(Theme));
            foreach (string theme in themes)
            {
                if (theme.ToLower() == themeName)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region IP限制登录
        /// <summary>
        /// 提示信息
        /// </summary>
        const string ErrorHtml = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""><html><head><title>系统提示</title><style type=""text/css"">.ipforbidden
{
    border: #c0c0c0 1px dashed;
    font-size: 9pt;
    line-height: 20pt;
    padding-left: 10px;
    padding-top: 5px;
    padding-right: 10px;
    padding-bottom: 10px;
    margin-top: 15px;
    margin-bottom: 15px;
}</style></head><body><div class=""ipforbidden"">您的访问受到限制，请与系统管理员联系。</div></body></html>";

        private void IPPross()
        {
            if (HttpContext.Current.Request.UserHostAddress != "115.238.160.198" && ConfigHelper.GetWebConfigPath("IPPass").Contains("," + CookInfo.EAdminGroupId().ToString() + ","))
            {
                HttpContext.Current.Response.Write(ErrorHtml);
                HttpContext.Current.Response.End();

            }
        }
        #endregion

        #region 错误处理
        //错误处理 
        protected void PageBase_Error(object sender, System.EventArgs e)
        {
            string errMsg = string.Empty;
            Exception currentError = HttpContext.Current.Server.GetLastError();
            if (currentError.ToString() != "")
            {
                DLog.WriteLog(DMessage.ERR, currentError);
                Response.Write("<script language='javascript'>alert('系统异常,请稍候再试！');top.location.href ='/tbs_Login.aspx';</script>");
                Response.End();
            }
        }
        #endregion
    }
}